package com.supermap.dataservice.cloud.cust.user;

import com.supermap.dubbo.api.cloud.cust.role.TreeNodeList;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @desc
 * @date 2019年03月04 15:07
 * @email 1301584033@qq.com
 */
@Mapper
public interface SysUserMapper {

    @Select("select menuid from sys_user_order_menu where userid=#{userId}")
    List<Integer> getSysMenuId(@Param("userId")Integer userId);
    @Select("select t1.id from sys_menu t1\n" +
            "inner join sys_area_menu t2 \n" +
            "on t1.id=t2.menuid and t1.state=0\n" +
            "inner join sys_area_role t3\n" +
            "on t2.areacode=t3.areacode\n" +
            "and t3.id=#{roleId}  and t3.areacode=#{areaCode} ")
    List<Integer> getAreaRole(@Param("roleId")Integer roleId,@Param("areaCode")String areaCode);


    @InsertProvider(type = SysUserMapperSQL.class,method = "putMenuISQL")
    Integer putMenuI(@Param("list") List<Integer> list,@Param("userId")Integer userId);


    @Insert("Insert into sys_user_role(userid,roleid) values(#{userId},#{role})")
    Integer putUserAreaRole(@Param("userId")Integer userId,@Param("role")Integer role);

    @Delete("delete from sys_user_role where userid=#{userId}")
    Integer delUserAreaRole(@Param("userId")Integer userId);

    @Delete("delete from sys_user_menu where userid=#{userId}")
    Integer delUserMenu(@Param("userId")Integer userId);

    @Select("select t2.id,t1.name,(select count(1) from sys_user_role where  userid=#{userId} and roleid=t2.id) as state from t_role t1\n" +
            "                    inner join sys_area_role t2\n" +
            "                    on t1.id=t2.roleid\n" +
            "                    inner join sys_area_role_menu t3\n" +
            "                    on t2.id=t3.arearoleid  where 1=1 and t1.del_flg=0  and t2.areacode=#{areaCode} ")
    List<TreeNodeList> getAllAreaRoleByRoleId(@Param("areaCode")String  areaCode,@Param("userId")Integer  userId);

    class  SysUserMapperSQL{
        public String  putMenuISQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            List<Integer> list=(List<Integer>)map.get("list");
            Integer userId=(Integer)map.get("userId");
            stringBuilder.append("insert into sys_user_menu(menuid,userid) values ");
            for (Integer menu:list){
                stringBuilder.append("("+menu+","+userId+"),");
            }

            return stringBuilder.toString().substring(0,stringBuilder.length()-1);
        }
    }
}
